// 导入Promise
import {
	request
} from '../../request/index.js'

Page({
	data: {
		tabs: [
			{
				id: 0,
				value: '全部',
				isActive: true
			},
			{
				id: 1,
				value: '待付款',
				isActive: false
			},
			{
				id: 2,
				value: '待发货',
				isActive: false
			},
			{
				id: 3,
				value: '退款/退货',
				isActive: false
			}
		]
	},
	handleTabsItemChange(e) {
		// 赋值
		const index = e.detail
		// 修改原数组
		this.handleTitleByIndex(index)
	},
	onShow(){
		
		const token = wx.getStorageSync("token")
		// 没有token值
		if(!token){
			wx.navigateTo({
				url:"/pages/auth/index"
			})
			
			return;
		}
		
		let res = getCurrentPages()
		let {type} = res[res.length-1].options
		this.getOrder(type)
		this.handleTitleByIndex(type-1)
	},
	async getOrder(type){
		
		const res = await request({
			url:"/my/orders/all",
			data:{type}
		})
		
		let orders = res.data.message.orders
		orders = orders.map(item => ({
			...item,
			create_time_cn:(new Date(item.create_time*1000)).toLocaleString()
		}))
		
		this.setData({
			orders
		})
	},
	handleTitleByIndex(index){
		let {tabs} = this.data
		
		tabs.forEach((item, index2) => {
			index2 === index ?
			item.isActive = true :
			item.isActive = false;
		})
		// 赋值
		this.setData({
			tabs
		})
	}
	
		
})
